﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TPA.DataAccess;
using System.Linq.Expressions;

namespace TVProgramAnalyst.TimeLine
{
    public partial class frmProgramSearchCondition : Form
    {
        public IQueryable<ProgramDisplayer> BoundProgramList
        {
            get;
            set;
        }

        Filter _filter;
        public Filter NewFilter
        {
            get{return _filter;}
        }

        public frmProgramSearchCondition()
        {
            InitializeComponent();
        }

        private void frmProgramSearchCondition_Load(object sender, EventArgs e)
        {
            cmbProperty.DataSource = typeof(ProgramDisplayer).GetProperties().Where(p => p.GetCustomAttributesData().Count > 1 ? p.GetCustomAttributesData()[1].ConstructorArguments[0].Value.ToString() == "true" : true).Select(p => new ListItem() { Text = p.GetCustomAttributesData().Count > 0 ? p.GetCustomAttributesData()[0].ConstructorArguments[0].Value.ToString() : p.Name, Value = p.Name }).ToList();
            cmbProperty.ValueMember = "Value";
            cmbProperty.DisplayMember = "Text";
            cmbProperty.SelectedIndex = cmbProperty.Items.Count>0?0:-1;
            cmbConnector.DataSource = typeof(FilterConnectorType).GetEnumNames();
        }

        private void cmbProperty_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbProperty.SelectedItem.ToString() != "Name" && cmbProperty.SelectedItem.ToString() != "EnglishName")
            {
                BindValues(LoadPropertyValues(cmbProperty.SelectedItem.ToString()));
                clbValues.Visible = true;
                txtValues.Visible = false;
            }
            else
            {
                clbValues.Visible = false;
                txtValues.Visible = true;
            }
        }

        IEnumerable<string> LoadPropertyValues(string property)
        {
            Expression<Func<ProgramDisplayer,string>> ex=LambdaExpressionHelper.SelectExpression(property);
            IQueryable<ProgramDisplayer> pds=BoundProgramList;
            return pds.Select(ex).Distinct();
        }

        void BindValues(IEnumerable<string> values)
        {
            clbValues.Items.Clear();
            clbValues.Items.AddRange(values.ToArray());
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            _filter = new Filter() { PropertyName = cmbProperty.SelectedItem.ToString(), Connector = (FilterConnectorType)Enum.Parse(typeof(FilterConnectorType), cmbConnector.Text) };
            if (cmbProperty.SelectedItem.ToString() != "Name" && cmbProperty.SelectedItem.ToString() != "EnglishName")
            {
                foreach (string cb in clbValues.CheckedItems)
                {
                    _filter.Values.Add(cb);
                }
            }
            else
            {
                _filter.Values.Add(txtValues.Text);
            }
            
            this.Close();
        }

    }
}
